﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using FullCoreDBModels;
using MyApp.DataModels;
using MyApp.IRepository;
using MyApp.Common;
using System.Data;
using MyApp.DataModels.DTO;
using AutoMapper;
using MyApp.WebApi.Common;
using MyApp.IService;

namespace MyApp.WebApi.Controllers
{
    /// <summary>
    /// 接口工具控制器
    /// </summary>
    public class InterfaceToolController : NoAuthApiControllerBase
    {
        IInterfaceToolService service;
        IMapper mapper;
        public InterfaceToolController(IInterfaceToolService repository, IMapper mapper)
        {
            //InterfaceToolRepository repository;
            this.service = repository;
            this.mapper = mapper;
        }
        #region MyRegion

        [HttpPost]
        public string Test()
        {
            LogHelper.GetLog().Error("测试错误消息");
            //repository.Insert();
            return service.Test();
        }
        /// <summary>
        /// 测试数据库
        /// </summary>
        /// <param name="repository"></param>
        /// <returns></returns>
        [HttpGet]
        public InterfaceToolEntityDTO QuerySingleByPK(string PK)
        {
            NLogHelper.GetLog().Info("测试一条日志");
            var data = service.QuerySingleByPK(PK);
            var res = mapper.Map<InterfaceToolEntityDTO>(data);
            return res;
        }

        /// <summary>
        /// 测试数据库
        /// </summary>
        /// <param name="repository"></param>
        /// <returns></returns>
        [HttpGet]
        public DataTable GetDataTable(string sql)
        {
            return service.GetDataTable(sql);
        }
        /// <summary>
        /// 测试数据库
        /// </summary>
        /// <param name="repository"></param>
        /// <returns></returns>
        [HttpGet]
        public int ExecuteCommand(string sql)
        {
            return service.ExecuteCommand(sql);
        }

        #endregion
        /// <summary>
        ///     获取接口工具数据信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public List<InterfaceToolEntity> GetInterfaceTool()
        {
            var conditions = new List<ConditionArgs>();
            return service.GetInterfaceTool(conditions);
        }

        /// <summary>
        ///     获取接口工具数据信息
        /// </summary>
        /// <param name="interfaceCode">接口编码</param>
        /// <returns></returns>
        [HttpGet]
        public InterfaceToolEntity GetInterfaceToolByCode(string interfaceCode)
        {
            var conditions = new List<ConditionArgs>();
            conditions.Add(new ConditionArgs()
            {
                Key = "INTERFACECODE",
                Value = interfaceCode,
                JudgeType = JudgmentEnum.Equal,
                PreConnector = ConnectorEnum.And
            });
            return service.GetInterfaceTool(conditions).FirstOrDefault();
        }

        /// <summary>
        ///     保存接口数据信息
        /// </summary>
        /// <param name="interfaceToolList"></param>
        /// <returns></returns>
        [HttpPost]
        public CommonResult SaveInterfaceTool([FromBody] List<InterfaceToolEntity> interfaceToolList)
        {
            var result = service.SaveInterfaceTool(interfaceToolList);
            return result;
        }
    }
}
